
import com.microsoft.schemas._2003._10.serialization.arrays.ArrayOfKeyValueOfanyTypeanyType;
import exceptions.ubf.ufsoft.ArrayOfMessageBase;
import model.RtnTokenResult;
import org.datacontract.schemas._2004._07.ufsoft_ubf_util.ThreadContext;
import org.tempuri.PersonADDSVStub;
import org.ufida.UFIDAU9CBOHRCBOHRSVIPersonADDSV;
import org.ufida.entitydata.ArrayOfUFIDAU9CBOHRCBOHRSVPersonDTOData;
import org.ufida.entitydata.ObjectFactory;
import org.ufida.entitydata.UFIDAU9CBOHRCBOHRSVPersonDTOData;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.ws.Holder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson.JSON;

public class ErpApiDemo {
    public static void main(String[] args) {

        testDemo("010207","erp测试所用2");
    }

    public static void testDemo(String deptCode, String deptName){
        String token=httpEprGet("http://192.168.1.17/U9C/webapi/OAuth2/AuthLogin?");
        String url="http://192.168.1.17/U9C/webapi/Department/Create";
        Map<String,Object> params=new HashMap<>();
        Map<String,Object> data=new HashMap<>();
        params.put("Code",deptCode);
        params.put("Name",deptName);
        data.put("Code","01");
        params.put("Org",data);
        String json=JSON.toJSONString(params);
        String result=HttpUtil.sendPost(url,"["+json+"]",token);
        System.out.println(result);
    }

    public static String httpEprGet(String strUrl) {
        Map<Object, Object> params = new HashMap<Object, Object>();
        params.put("entCode", "3000");
        params.put("userCode", "admin");
        params.put("orgCode", "01");
        params.put("clientid", "API");
        params.put("clientsecret", "956a897e2dfe4615863473355178baf3");
        String url = "";
        try {
            url = strUrl + getErpUrlData(params);
        } catch (Exception e) {
            e.printStackTrace();
        }

        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e2) {
                }
            }
        }

        RtnTokenResult token= JSON.parseObject(result.toString(), RtnTokenResult.class);
        return token.Data;
    }
    public static String getErpUrlData(Map<Object, Object> data) throws Exception {
        StringBuffer sb = new StringBuffer();
        try {
            Set<Map.Entry<Object, Object>> entries = data.entrySet();
            Iterator<Map.Entry<Object, Object>> iterators = entries.iterator();
            while (iterators.hasNext()) {
                Map.Entry<Object, Object> next = iterators.next();
                sb.append(next.getKey().toString().trim()).append("=").append(URLEncoder.encode(next.getValue() + "", "UTF-8").trim()).append("&");
            }
            sb.deleteCharAt(sb.length() - 1);
        } catch (Exception e) {
            sb.append(e.toString());
        }
        return sb.toString();
    }

    public static  String sendWebservice() throws ParseException, DatatypeConfigurationException{
        PersonADDSVStub personADDSVStub=new PersonADDSVStub();
        UFIDAU9CBOHRCBOHRSVIPersonADDSV personADDSV=personADDSVStub.getBasicHttpBindingUFIDAU9CBOHRCBOHRSVIPersonADDSV();
        org.ufida.entitydata.ObjectFactory objectFactory=new ObjectFactory();

        ArrayOfUFIDAU9CBOHRCBOHRSVPersonDTOData arr=objectFactory.createArrayOfUFIDAU9CBOHRCBOHRSVPersonDTOData();

        UFIDAU9CBOHRCBOHRSVPersonDTOData headerData=objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTOData();
        //headerData.setSysState(ObjectState.INSERTED);
        //设置时间
        GregorianCalendar assgnBeginDate =new GregorianCalendar();
        //指定日期格式
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //创建日期变量
        assgnBeginDate.setTime(simpleDateFormat.parse("2023-01-02 00:00:00"));

        //类的一个对象来表示一个时间，该对象封装了一个具体时间的年、月、日、时、分、秒及时区（Time Zone）信息
        XMLGregorianCalendar assgnBeginDateXMLGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(assgnBeginDate);
        //任职开始日期
        headerData.setMAssgnBeginDate(assgnBeginDateXMLGregorianCalendar);
        System.out.println(assgnBeginDateXMLGregorianCalendar);
        //设置时间
        GregorianCalendar startDate =new GregorianCalendar();

        startDate.setTime(simpleDateFormat.parse("2022-01-01 00:00:00"));

        XMLGregorianCalendar startDateXMLGregorianCalendar= DatatypeFactory.newInstance().newXMLGregorianCalendar(startDate);
        //就业日期
        headerData.setMStartDate(startDateXMLGregorianCalendar);
        //现任业务组织编码
        headerData.setMBusinessOrgCode(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMBusinessOrgCode("01"));
        //身份证号码
        headerData.setMCertificateType(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMCertificateType("441823199606060606"));
        //创建组织编码
        headerData.setMCreateOrgCode(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMCreateOrgCode("01"));
        //部门编码
        headerData.setMDeptCode(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMDeptCode("01010203"));

        //员工类型编码
        headerData.setMEmployeeCategoryCode(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMEmployeeCategoryCode("01"));
        //名
        headerData.setMFirstName(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMFirstName("张"));
        //姓
        headerData.setMLastName(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMLastName("三"));
        //人事组织编码
        headerData.setMHROrgCode(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMHROrgCode("01"));
        //现任职务编码
        headerData.setMJobCode(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMJobCode("01"));
        //人事归属组织
        headerData.setMOwnerOrgCode(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMOwnerOrgCode("01"));
        //员工编码
        headerData.setMPersonCode(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMPersonCode("01009999"));
        //任职岗位编码
        headerData.setMPositionCode(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMPositionCode("01"));
        //性别
        headerData.setMSex(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMSex("0"));
        //工作人事组织编码
        headerData.setMWorkingOrgCode(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMWorkingOrgCode("01"));
        //主键ID
        headerData.setId("354");
        //引用5364
        //headerData.setRef("");
        //国家
        //headerData.setMCountry(objectFactory.createUFIDAU9CBOHRCBOHRSVPersonDTODataMCountry("中国"));

        arr.getUFIDAU9CBOHRCBOHRSVPersonDTOData().add(headerData);

        Holder<String> doResult=new Holder<>();
        Holder<ArrayOfMessageBase> outMessages=new Holder<>();

        String IDForCompany="3000";
        Long IDForOrganization=1002211220110505L;
        Long IDForUsers=1002211220111698L;
        String UserCode="admin";

        Object context=HttpUtil.createErpContext(IDForCompany,IDForOrganization,IDForUsers,UserCode);


        try {
            personADDSV._do(context,arr,doResult,outMessages);
            return doResult.value;
        } catch (Exception e) {
            return e.getMessage();
        }

    }

}
